﻿using Spire.Doc;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Web;

namespace Solidinfo.Web.Helpers
{
    public static class PrintHelper
    {
        /// <summary>
        /// 获取所有安装的打印机名称
        /// </summary>
        /// <returns>打印机名称集合</returns>
        public static IEnumerable<string> GetAllInstalledPrinter()
        {
            var printers = PrinterSettings.InstalledPrinters;
            foreach (string item in printers)
            {
                yield return item;
            }
        }
        /// <summary>
        /// 获取默认打印机名称
        /// </summary>
        /// <returns>打印机名称</returns>
        public static string GetDefaultPrinter()
        {
            PrintDocument a = new PrintDocument();
            return a.PrinterSettings.PrinterName;
        }
        /// <summary>
        /// 打印Doc文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        public static void Print(string fileName)
        {
            Print(fileName, null);
        }
        /// <summary>
        /// 打印文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="printerName">打印机名称</param>
        public static void Print(string fileName, string printerName)
        {
            var extensionName = Path.GetExtension(fileName).ToLower();
            switch (extensionName)
            {
                case ".doc":
                case ".docx":
                    Print(fileName, PrintSupportedFileType.DocOrDocx, printerName);
                    break;
                case ".xls":
                case ".xlsx":
                    Print(fileName, PrintSupportedFileType.XlsOrXlsx, printerName);
                    break;
                case ".pdf":
                    Print(fileName, PrintSupportedFileType.Pdf, printerName);
                    break;
                default:
                    throw new NotSupportedException(string.Format("不支持后缀名为{0}的文件类型进行打印", extensionName));
            }

        }
        /// <summary>
        /// 打印文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="fileType">打印支持的文件类型</param>
        /// <param name="printerName">打印机名称</param>
        public static void Print(string fileName, PrintSupportedFileType fileType, string printerName)
        {
            switch (fileType)
            {
                case PrintSupportedFileType.DocOrDocx:
                    PrintDocFile(fileName, printerName);
                    break;
                case PrintSupportedFileType.XlsOrXlsx:
                    PrintXlsFile(fileName, printerName);
                    break;
                case PrintSupportedFileType.Pdf:
                    PrintPdfFile(fileName, printerName);
                    break;
                default:
                    throw new NotSupportedException(string.Format("不支持{0}的文件类型进行打印", fileType));
            }
        }
        /// <summary>
        /// 打印DOC文件
        /// </summary>
        /// <param name="docName">文件路径</param>
        /// <param name="printerName">打印机名称</param>
        public static void PrintDocFile(string docName, string printerName = null)
        {
            using (Document document = new Document(docName))
            {
                if (!string.IsNullOrEmpty(printerName))
                {
                    document.PrintDocument.PrinterSettings.PrinterName = printerName;
                }
                document.PrintDocument.PrintController = new StandardPrintController();
                document.PrintDocument.Print();
            }
        }
        /// <summary>
        /// 打印XLS文件
        /// </summary>
        /// <param name="xlsName">文件路径</param>
        /// <param name="printerName">打印机名称</param>
        public static void PrintXlsFile(string xlsName, string printerName = null)
        {
            using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook())
            {
                workbook.LoadFromFile(xlsName);
                if (!string.IsNullOrEmpty(printerName))
                {
                    workbook.PrintDocument.PrinterSettings.PrinterName = printerName;
                }
                workbook.PrintDocument.PrintController = new StandardPrintController();
                workbook.PrintDocument.Print();
            }
        }
        /// <summary>
        /// 打印PDF文件
        /// </summary>
        /// <param name="pdfName">文件路径</param>
        /// <param name="printerName">打印机名称</param>
        public static void PrintPdfFile(string pdfName, string printerName = null)
        {

            using (Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument(pdfName))
            {
                if (!string.IsNullOrEmpty(printerName))
                {
                    document.PrintDocument.PrinterSettings.PrinterName = printerName;
                }
                document.PrintDocument.PrintController = new StandardPrintController();
                document.PrintDocument.Print();
            }
        }


    }
    /// <summary>
    /// 打印支持的枚举类型
    /// </summary>
    public enum PrintSupportedFileType
    {
        /// <summary>
        /// DOC、DOCX文件
        /// </summary>
        DocOrDocx,
        /// <summary>
        /// XLS、XLSX文件
        /// </summary>
        XlsOrXlsx,
        /// <summary>
        /// PDF文件
        /// </summary>
        Pdf
    }
}